﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.UI.WebControls;
using Weixin.MP.UI;

namespace Weixin.MP.Web.Controllers
{
    public class VerifyImgController : Controller
    {
        private Bitmap validateimage;
        private Graphics g;

        public ActionResult Index()
        {
            string code = MakeValidateCode();
            Session[Common.VerifyCodeSession] = code;
            var bytes = CreateValidateGraphic(code);
            return File(bytes, @"image/jpeg");
        }

        public byte[] CreateValidateGraphic(string validCode)
        {
            try
            {
                //字义图片大小
                validateimage = new Bitmap(75, 24);
                g = Graphics.FromImage(validateimage);
                WebColorConverter ww = new WebColorConverter();
                g.Clear((Color)ww.ConvertFromString("#e2e5e8"));
                Random rnd = new Random();
                //画图片的背景噪音线
                for (int i = 0; i < 12; i++)
                {
                    int x1 = rnd.Next(validateimage.Width);
                    int x2 = rnd.Next(validateimage.Width);
                    int y1 = rnd.Next(validateimage.Height);
                    int y2 = rnd.Next(validateimage.Height);
                    g.DrawLine(new Pen(Color.LightGray), x1, y1, x2, y2);
                }
                Font font = new Font("Arial", 12, FontStyle.Bold | FontStyle.Italic);
                LinearGradientBrush brush = new LinearGradientBrush(
                    new Rectangle(0, 0, validateimage.Width, validateimage.Height), Color.DarkRed, Color.DarkGreen, 1.5f, true);
                g.DrawString(validCode, font, brush, 5, 0);
                //画图片的前景噪音点 
                for (int i = 0; i < 10; i++)
                {
                    int x = rnd.Next(validateimage.Width);
                    int y = rnd.Next(validateimage.Height);
                    validateimage.SetPixel(x, y, Color.White);
                }

                g.DrawRectangle(new Pen(Color.Silver), 0, 0, validateimage.Width - 1, validateimage.Height - 1);
                //g.FillRectangle(new LinearGradientBrush(new Point(0,0), new Point(110,20), Color.FromArgb(240,255,255,255),Color.FromArgb(240,255,255,255)),0,0,200,200); 
                //g.DrawString(VNum, new Font("arial", 11), new SolidBrush(Color.Black), new PointF(6, 0));

                //g.Save();
                MemoryStream ms = new MemoryStream();
                validateimage.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);

                return ms.ToArray();
                //Response.ClearContent();
                //Response.ContentType = "image/gif";
                //Response.BinaryWrite(ms.ToArray());
                //g.Dispose();
                //validateimage.Dispose();
                //Response.End();
            }
            finally
            {
                g.Dispose();
                validateimage.Dispose();
            }
        }

        string MakeValidateCode()
        {
            string str = "23456789ABCDEFGHJKLMNPQRSTUVWXYZ";

            //char[] s = new char[]{'0','1', '2','3','4','5','6','7','8','9',};
            string num = "";
            Random r = new Random();
            for (int i = 0; i < 5; i++)
            {
                num += str[r.Next(0, str.Length)].ToString();
            }
            return num;
        }
    }
}
